<template>
  <div class="app-content-wrap" :style="'minHeight:' + gHeight + 'px;'">
    <div class="g-content-title">
      管理员
      <div class="department-top-btn">
        <!-- <a class="btn-default" @click="addUser()">新增人员</a> -->
        <el-button class="btn-default" type="primary" plain icon="el-icon-plus" size="mini" @click="addUser()">新增</el-button>
      </div>
    </div>
    <!-- 搜索框 -->
    <el-form :model="userFilter" label-width="80px" size="small">
      <el-row :gutter="15">
        <el-col :span="5">
          <el-form-item label="姓名">
            <el-input v-model="userFilter.userName" placeholder="姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="电话">
            <el-input v-model="userFilter.mobile" placeholder="电话"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="所属小区">
            <AreaSelect :selectModel="userFilter.deptId" :areaIds="areaIds" @onAreaChange="onAreaChange" hideThird="true">
            </AreaSelect>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item>
            <el-button type="primary" size="small" icon="el-icon-search" @click="getUserFromFilter">搜 索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 列表 -->
    <div style="margin:10px 30px;">
      <el-table :data="userListTable" border style="width: 100%">
        <el-table-column prop="userName" label="姓名" min-width="100" align="center"></el-table-column>
        <el-table-column prop="gender" label="性别" min-width="120" align="center"></el-table-column>
        <el-table-column prop="cardNo" label="身份证号" min-width="140" align="center"></el-table-column>
        <el-table-column prop="mobile" label="电话" min-width="140" align="center"></el-table-column>
        <el-table-column prop="deptList" label="小区" min-width="140" :formatter="transformDeptName"></el-table-column>
        <el-table-column label="操作" width="190" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="userModify(scope.row)">编辑</el-button>
            <el-button type="text" @click="userRemove(scope.row)">移除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination background @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-size="10" layout="total, prev, pager, next,jumper" :total="pagination.total" class="el-page-body"></el-pagination>
    </div>
  </div>
</template>
<script>
import AreaSelect from '../../components/common/select/area.vue'

export default {
  name: 'appletManager',
  components: {
    AreaSelect
  },
  data () {
    return {
      gHeight: this.$store.state.gHeight,
      areaIds: {
        areaId1: '',
        areaId2: '',
        areaId3: ''
      },
      userFilter: {
        userName: '', // 姓名
        mobile: '', // 电话
        deptId: '' // 部门
      },
      userListTable: [],
      pagination: {
        currentPage: 1,
        total: null
      }
    }
  },
  methods: {
    transformDeptName (row) {
      let depts = []
      row.deptList.forEach((item) => {
        depts.push(item.deptName)
      })
      return depts.join('，')
    },
    addUser () {
      this.$router.push({
        name: 'userManager',
        params: {
          userId: 'addUser'
        }
      })
    },
    userDetail: function (row) {
      this.$router.push({
        name: 'userDetail',
        params: {
          userId: row.userId
        }
      })
    },
    userModify (row) {
      this.$router.push({
        name: 'userManager',
        params: {
          userId: row.userId
        }
      })
    },
    // 移除管理员
    userRemove (row) {
      let userId = row.userId
      this.$confirm('移除该管理员?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleRemoveManager(userId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleRemoveManager (userId) {
      const url = this.$base.getUrl('appletRemoveManager')
      this.$store.dispatch('postForm', {
        isJson: true,
        url: url,
        data: {
          userId: userId
        }
      }).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getUser()
        }
      })
    },
    getUserFromFilter: function () { /// /表单
      this.getUser()
    },
    handleCurrentChange (currPage) {
      this.pagination.currentPage = currPage
      this.getUser()
    },
    getUser () {
      const url = this.$base.getUrl('appletGetManagerList')
      let params = {
        isJson: true,
        url: url,
        data: {
          userName: this.userFilter.userName, // 姓名
          mobile: this.userFilter.mobile, // 电话
          deptId: this.userFilter.deptId, // 部门
          page: this.pagination.currentPage,
          limit: 10
        }
      }
      this.$store.dispatch('postForm', params).then((res) => {
        let { result } = res
        this.userListTable = result.list
        this.pagination.currentPage = result.currPage
        this.pagination.total = result.totalCount
      })
    },
    // area组件change时
    onAreaChange (val) {
      this.userFilter.deptId = val
    }
  },
  filters: {

  },
  mounted () {
    this.getUser()
  }
}
</script>
<style lang='less' scoped>
.g-content-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.department-top-btn {
  float: right;
}

.department-top-btn .btn-default {
  display: inline-block;
  padding: 2px 12px;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.42857143;
  text-align: center;
  cursor: pointer;
  background: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-right: 30px;
}
.el-table {
  width: auto !important;
  text-align: center;
  margin: 10px;
}

/deep/ .el-table th {
  text-align: center;
}
</style>
